import * as types from '../types';
import axios from 'axios';


export default {
  state: {
    access_token: '',
    refresh_token: '',
    expire_access: NaN,
    expire_refresh: NaN,
    userid: NaN,
    email: '',
    create_time: '',
    last_login: '',
    avatar: '',
  },
  getters: {
    isLogin: state => {
      return state.access_token !== "";
    },
    isTokenSync: state => {
      if (state.access_token === '' && window.localStorage.isSavedToken === "1") {
        return false;
      }
      return true;
    },
    access_token: state => {
      return state.access_token;
    },
    email: state => state.email,
    userName: state => {
      if (state.email === "") {
        return;
      }
      let at_pos = state.email.indexOf("@");
      if (at_pos == -1) {
        return;
      }
      return state.email.slice(0, at_pos);
    }
  },
  mutations: {
    [types.LOGIN]: (state, payload) => {
      state.expire_access = payload.expire_access;
      state.expire_refresh = payload.expire_refresh;
      state.access_token = payload.access_token;
      state.refresh_token = payload.refresh_token;
      state.userid = payload.userid;
      state.email = payload.email;
      state.create_time = payload.create_time;
      state.last_login = payload.last_login;
      state.avatar = payload.avatar;
      window.localStorage.auth = JSON.stringify(state);
      window.localStorage.isSavedToken = "1";
    },
    [types.LOGOUT]: (state) => {
      state.access_token = '';
      delete window.localStorage.auth;
      delete window.localStorage.isSavedToken;
    },
    [types.TOKEN_SYNC]: (state) => {
      console.assert(window.localStorage.isSavedToken === "1");
      console.assert(window.localStorage.auth !== "");
      let payload = JSON.parse(window.localStorage.auth);
      state.expire_access = payload.expire_access;
      state.expire_refresh = payload.expire_refresh;
      state.access_token = payload.access_token;
      state.refresh_token = payload.refresh_token;
      state.userid = payload.userid;
      state.email = payload.email;
      state.create_time = payload.create_time;
      state.last_login = payload.last_login;
      state.avatar = payload.avatar;
    }
  },
  actions: {
    login: ({ commit }, payload) => {
      return new Promise((resolve, reject) => {
        axios.post('/login/', payload).then(response => {
          commit(types.LOGIN, response.data);
          commit(types.TOKEN_SYNC);
          resolve(response.data);
        }).catch(err => {
          console.log(err);
          reject(err);
        });
      });
    },
    logout: ({ commit }) => {
      axios.post('/logout/').then(response => {
        commit(types.LOGOUT);
      }).catch(err => {
        console.log('logout error.');
        commit(types.LOGOUT);
      });
    },
    register: ({ commit }, payload) => {
      return new Promise((resolve, reject) => {
        axios.post('/register/', payload).then(response => {
          commit(types.LOGIN, response.data);
          commit(types.TOKEN_SYNC);
          resolve(response.data);
        }).catch(err => {
          console.log(err);
          reject(err);
        });
      });
    }
  }
}